package com.example.yyang.demo

import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.MenuItem
import com.example.yyang.demo.adapter.MainPagerAdapter
import com.example.yyang.demo.fragment.FindFragment
import com.example.yyang.demo.fragment.MainFragment
import com.example.yyang.demo.fragment.MeFragment
import com.example.yyang.demo.fragment.MoreFragment
import kotlinx.android.synthetic.main.activity_launch.*

class LaunchActivity : AppCompatActivity() {
    private lateinit var menuItem: MenuItem
    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        menuItem = item
        when (item.itemId) {
            R.id.navigation_home -> {
//                message.setText(R.string.title_home)
                viewpager.currentItem = 0
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_find -> {
                viewpager.currentItem = 1
//                message.setText(R.string.title_dashboard)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_more -> {
                viewpager.currentItem = 2
//                message.setText(R.string.title_notifications)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_me -> {
                viewpager.currentItem = 3
//                message.setText(R.string.title_notifications)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_launch)
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        val fragments = listOf<Fragment>(MainFragment(),FindFragment(),MoreFragment(),MeFragment())
        viewpager.adapter = MainPagerAdapter(supportFragmentManager,fragments)
        viewpager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onPageSelected(p0: Int) {
                if (menuItem != null){
                    menuItem!!.isChecked = false
                }else{
                    navigation.menu.getItem(0).isChecked = false
                }
                menuItem = navigation.menu.getItem(p0)
                menuItem!!.isChecked = true
            }

        })

    }
}
